/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.logistics.web.inboundorder;

import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.base.entity.cars.Tcar;
import com.jeesite.modules.base.entity.warehouse.Twarehouse;
import com.jeesite.modules.base.service.cars.TcarService;
import com.jeesite.modules.base.service.warehouse.TwarehouseService;
import com.jeesite.modules.logistics.entity.inboundorder.InboundOrder;
import com.jeesite.modules.logistics.entity.outboundorder.OutboundOrder;
import com.jeesite.modules.logistics.service.inboundorder.InboundOrderService;
import com.jeesite.modules.logistics.service.outboundorder.OutboundOrderService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrder;
import com.jeesite.modules.order.service.serviceorder.ServiceOrderService;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.service.EmpUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 入库单Controller
 * @author LV
 * @version 2021-03-10
 */
@Controller
@RequestMapping(value = "${adminPath}/logistics/inboundorder/inboundOrder")
public class InboundOrderController extends BaseController {

	@Autowired
	private InboundOrderService inboundOrderService;
    @Autowired
    private ServiceOrderService serviceOrderService;
    @Autowired
    private TwarehouseService warehouseService;
    @Autowired
    private EmpUserService empUserService;
    @Autowired
    private TcarService carService;
    @Autowired
    private OutboundOrderService outboundOrderService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public InboundOrder get(String inboundOrderId, boolean isNewRecord) {
		return inboundOrderService.get(inboundOrderId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("logistics:inboundorder:inboundOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(InboundOrder inboundOrder, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));
        model.addAttribute("listWarehouse", warehouseService.findList(new Twarehouse()));
        model.addAttribute("listCar", carService.findList(new Tcar()));
        model.addAttribute("listUser", empUserService.findList(new EmpUser()));
		model.addAttribute("inboundOrder", inboundOrder);
		return "modules/logistics/inboundorder/inboundOrderList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("logistics:inboundorder:inboundOrder:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<InboundOrder> listData(InboundOrder inboundOrder, HttpServletRequest request, HttpServletResponse response) {
		inboundOrder.setPage(new Page<>(request, response));
		Page<InboundOrder> page = inboundOrderService.findPage(inboundOrder);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("logistics:inboundorder:inboundOrder:view")
	@RequestMapping(value = "form")
	public String form(InboundOrder inboundOrder, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));
        model.addAttribute("listWarehouse", warehouseService.findList(new Twarehouse()));
        model.addAttribute("listCar", carService.findList(new Tcar()));
		model.addAttribute("inboundOrder", inboundOrder);
		return "modules/logistics/inboundorder/inboundOrderForm";
	}

	/**
	 * 查看表单详情
	 */
	@RequiresPermissions("logistics:inboundorder:inboundOrder:view")
	@RequestMapping(value = "detail")
	public String detail(InboundOrder inboundOrder, Model model) {
        model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));
        model.addAttribute("listWarehouse", warehouseService.findList(new Twarehouse()));
        model.addAttribute("listCar", carService.findList(new Tcar()));
		model.addAttribute("inboundOrder", inboundOrder);
		return "modules/logistics/inboundorder/inboundOrderDetail";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("logistics:inboundorder:inboundOrder:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated InboundOrder inboundOrder) {
		inboundOrderService.save(inboundOrder);
		return renderResult(Global.TRUE, text("保存入库单成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("logistics:inboundorder:inboundOrder:delete")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(InboundOrder inboundOrder) {
		inboundOrderService.delete(inboundOrder);
		return renderResult(Global.TRUE, text("删除入库单成功！"));
	}
	
	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("logistics:inboundorder:inboundOrder:view")
	@RequestMapping(value = "inboundOrderSelect")
	public String empUserSelect(InboundOrder inboundOrder, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("InboundOrder", inboundOrder);
		return "modules/logistics/inboundorder/inboundOrderSelect";
	}

    /**
     * 创建自出库单
     */
    @RequiresPermissions("logistics:inboundorder:inboundOrder:view")
    @RequestMapping(value = "createFromOutbound")
    public String createFromOutbound(OutboundOrder outboundOrder, Model model) {
        InboundOrder inboundOrder = new InboundOrder();
        OutboundOrder oldOutboundOrder = outboundOrderService.get(outboundOrder);
        if (oldOutboundOrder != null){
            inboundOrder.setServiceOrderId(oldOutboundOrder.getServiceOrderId());
            inboundOrder.setFrameNumber(oldOutboundOrder.getFrameNumber());
            inboundOrder.setCarId(oldOutboundOrder.getCarId());
            inboundOrder.setFromWarehouse(oldOutboundOrder.getFromWarehouse());
            inboundOrder.setOutboundDate(oldOutboundOrder.getDate());
            inboundOrder.setFromWarehouseType(oldOutboundOrder.getFromWarehouseType());
            inboundOrder.setOutboundOrderId(oldOutboundOrder.getOutboundOrderId());
            inboundOrder.setToWarehouseType(oldOutboundOrder.getToWarehouseType());
            inboundOrder.setToWarehouse(oldOutboundOrder.getToWarehouse());
            model.addAttribute("listServiceOrder", serviceOrderService.findList(new ServiceOrder()));
            model.addAttribute("listWarehouse", warehouseService.findList(new Twarehouse()));
            model.addAttribute("listCar", carService.findList(new Tcar()));
            model.addAttribute("inboundOrder", inboundOrder);
            return "modules/logistics/inboundorder/inboundOrderForm";
        }else{
            return renderResult(Global.FALSE, text("未查找到出库单的相关信息！"));
        }
    }


}